package club.zhilin.guli.service.edu.service.impl;

import club.zhilin.guli.common.base.result.CommonResult;
import club.zhilin.guli.service.edu.domain.Course;
import club.zhilin.guli.service.edu.domain.Teacher;
import club.zhilin.guli.service.edu.domain.qo.TeacherQueryObject;
import club.zhilin.guli.service.edu.feign.OssFileService;
import club.zhilin.guli.service.edu.mapper.CourseMapper;
import club.zhilin.guli.service.edu.mapper.TeacherMapper;
import club.zhilin.guli.service.edu.service.ITeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author zhilin
 * @since 2022-01-11 15:29:01
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements ITeacherService {

    private final OssFileService ossFileService;

    private final CourseMapper courseMapper;

    @Override
    public CommonResult<Map<String, Object>> selectList(Long page, Long limit, TeacherQueryObject teacherQueryObject) {
        // 显示分页查询列表
        Map<String, Object> result = new HashMap<>(2);
        Page<Teacher> teacherPage = new Page<>(page, limit);
        LambdaQueryWrapper<Teacher> lambdaQueryWrapper = null;
        // 根据查询条件查询
        if (teacherQueryObject != null) {
            lambdaQueryWrapper = new LambdaQueryWrapper<>();
            if (teacherQueryObject.getLevel() != null) {
                lambdaQueryWrapper.eq(Teacher::getLevel, teacherQueryObject.getLevel());
            }
            if (StringUtils.isNotBlank(teacherQueryObject.getJoinDateBegin())) {
                lambdaQueryWrapper.ge(Teacher::getJoinDate, teacherQueryObject.getJoinDateBegin());
            }
            if (StringUtils.isNotBlank(teacherQueryObject.getName())) {
                lambdaQueryWrapper.like(Teacher::getName, teacherQueryObject.getName());
            }
            if (StringUtils.isNotBlank(teacherQueryObject.getJoinDateEnd())) {
                lambdaQueryWrapper.le(Teacher::getJoinDate, teacherQueryObject.getJoinDateEnd());
            }
            if (StringUtils.isNotBlank(teacherQueryObject.getOrderBy())) {
                lambdaQueryWrapper.orderBy(true, "asc".equalsIgnoreCase(teacherQueryObject.getOrderBy()), Teacher::getSort);
            }
        }
        IPage<Teacher> iPage = this.baseMapper.selectPage(teacherPage, lambdaQueryWrapper);
        result.put("total", iPage.getTotal());
        result.put("rows", iPage.getRecords());
        return CommonResult.success(result);
    }

    @Override
    public List<Map<String, Object>> accordingKeySearchNameList(String key) {
        return this.baseMapper.selectMaps(new LambdaQueryWrapper<Teacher>().select(Teacher::getName).like(Teacher::getName, key));
    }

    @Override
    public boolean removeAvatarById(String id) {
        Teacher teacher = baseMapper.selectOne(new LambdaQueryWrapper<Teacher>().select(Teacher::getAvatar).eq(Teacher::getId, id));
        if (teacher != null) {
            String avatar = teacher.getAvatar();
            if (StringUtils.isNotEmpty(avatar)) {
                CommonResult<String> result = ossFileService.deleteFile(avatar);
                return result.isSuccess();
            }
        }
        return false;
    }

    @Override
    public Map<String, Object> selectCourseByTeacherId(String teacherId) {
        Teacher teacher = this.baseMapper.selectById(teacherId);
        List<Course> courses = courseMapper.selectList(new LambdaQueryWrapper<Course>().eq(Course::getTeacherId, teacherId));
        Map<String, Object> result = new HashMap<>(2);
        result.put("teacher", teacher);
        result.put("courseList", courses);
        return result;
    }

    @Override
    @Cacheable(value = "index", key = "'selectHotTeacher'")
    public List<Teacher> selectHotTeacher() {
        return baseMapper.selectList(new LambdaQueryWrapper<Teacher>().orderByDesc(Teacher::getSort).last("limit 4"));
    }
}
